/**
 * @(#)LoginManager.java        1.0 3/25/12
 * 
 * Copyright (c) 2012 public static droid main
 * Georgia Institute of Technology, Atlanta, GA
 * All rights reserved.
 */

package edu.gatech.oad.publicstaticdroidmain.taskmanager.model;

import edu.gatech.oad.publicstaticdroidmain.taskmanager.model.UserAccount;
import edu.gatech.oad.publicstaticdroidmain.taskmanager.persistence.DataSourceInterface;

/**
 * A class that facilitates logging in to the system.  Contains facilities
 * for creating a UserAccount object from a valid login, creating a user,
 * and verifying credentials.
 * 
 * @version 1.0 3/25/12
 * @author Raheel Merali
 *
 */
public class LoginManager {
    private DataSourceInterface database;
    
    /**
     * Constructs a new LoginManager using the specified DatabaseInterface.
     * 
     * @param database the database from which user information will be retrieved
     */
    public LoginManager(DataSourceInterface database) {
	this.database = database;
    }
    
    /**
     * Returns true if an account with the specified username exists in
     * the database.
     * 
     * @param username the username
     * @return true if the username exists in the database; false otherwise
     */
    public boolean userExists(String username) {
	return database.userExists(username);
    }
    
    /**
     * Returns true if the specified credentials represent a valid username-password
     * combination.
     * 
     * @param username the username
     * @param password the password
     * @return true if the credentials represent a valid username-password combination;
     * 		false otherwise
     */
    public boolean verifyCredentials(String username, String password) {
	return database.verifyCredentials(username, password);
    }
    
    /**
     * Generates a UserAccount object representing the account with the specified
     * credentials.
     * 
     * @param username the username
     * @param password the password
     * @return a UserAccount representing the specified account; null if the credentials
     * 		do not correspond to an actual account in the database
     */
    public UserAccount login(String username, String password) {
	UserAccount account = null;
	
	if (verifyCredentials(username, password)) {
	    account = new UserAccount(database, username);
	}
	
	return account;
    }
    
    /**
     * Creates a user in the database with the specified credentials.
     * 
     * @param username the username of the account to create
     * @param password the password of the account to create
     * @param realName the real-life name of the person whose account is being created
     * @param emailAddress the e-mail address of the person whose account is being created
     * @return true if the account was successfully created; false if an account with
     * 		the specified username already exists in the database
     */
    public boolean createUser(String username, String password, String realName, String emailAddress) {
	return database.createUser(username, password, realName, emailAddress);
    }
}
